//
//  LPSortOptionView.m
//  muce
//
//  Created by Pan on 2018/3/20.
//  Copyright © 2018年  yunshao. All rights reserved.
//

/** 排序项 */

#import "LPSortOptionView.h"
#import "pch.h"

#define WIDTH CGRectGetWidth(self.frame)

@interface LPSortOptionView ()

@property (nonatomic, strong) UILabel *optionLabel;

@end


@implementation LPSortOptionView


- (instancetype)initWithFrame:(CGRect)frame dictionary:(NSDictionary *)dictionary index:(NSInteger)index {
    if (self = [super initWithFrame:frame]) {
        _index = index;
        _dictioanry = dictionary;
        [self createUI];
    }
    
    return self;
}


- (void)createUI {
    
    // 选项
    self.layer.borderWidth = DSFixedPX(2);
    self.layer.borderColor = DSColorWithHex(0x6aa2e0).CGColor;
    self.userInteractionEnabled = YES;

    // 选项题
    _optionLabel = [self addLableWithMaxWidth:WIDTH - DSFixedPX(40) * 2 height:DSFixedPX(9999) string:[NSString stringWithFormat:@"%ld      %@", self.index + 1, _dictioanry[@"option_content"]] font:DSFontSize(14) textColor:DSColorWithHex(0x333333) numberOfLines:0 sizfTofit:YES];
    _optionLabel.center = CGPointMake(DSFixedPX(40) + CGRectGetWidth(_optionLabel.frame) / 2.0, DSFixedPX(16) + CGRectGetHeight(_optionLabel.frame) / 2.0);
    [self addSubview:_optionLabel];
    
    CGRect rect = self.frame;
    rect.size.height = CGRectGetMaxY(_optionLabel.frame) + DSFixedPX(16);
    self.frame = rect;
    self.midY = self.frame.size.height / 2.0;
}

- (void)setSortSelect:(BOOL)sortSelect {
    
    _sortSelect = sortSelect;
    if (sortSelect) {
        self.backgroundColor = DSColorWithHex(0xf6551e);
        _optionLabel.textColor = DSColorWithHex(0xffffff);
    }else {
        self.backgroundColor  = [UIColor clearColor];
        _optionLabel.textColor = DSColorWithHex(0x333333);
    }
}




/** Label标签 */
-(UILabel *)addLableWithMaxWidth:(CGFloat)maxWidth
                          height:(CGFloat)height
                          string:(NSString *)string
                            font:(UIFont *)font
                       textColor:(UIColor *)textColor
                   numberOfLines:(int)numberOfLines
                       sizfTofit:(BOOL)sizfTofit
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, maxWidth, height)];
    if ([string isKindOfClass:[NSNull class]]) {
        string = @"";
    }
    if (string.length <= 0) {
        string = @"";
    }
    label.text = string;
    
    label.font = font;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = textColor;
    label.numberOfLines = numberOfLines;
    [label sizeToFit];
    if (sizfTofit) {
        if (CGRectGetWidth(label.bounds) > maxWidth) {
            label.bounds = CGRectMake(0, 0, maxWidth, CGRectGetHeight(label.bounds));
        }
    }else{
        label.bounds = CGRectMake(0, 0, maxWidth, height);
    }
    return label;
}

@end
